package cn.tedu.crm.refund.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.temp.SaTempUtil;
import cn.tedu.crm.customer.mapper.CustomerFollowUpMapper;
import cn.tedu.crm.customer.pojo.entity.CustomerFollowUp;
import cn.tedu.crm.operatelog.loggable.Loggable;
import cn.tedu.crm.operatelog.loggable.OperationModule;
import cn.tedu.crm.operatelog.loggable.OperationType;
import cn.tedu.crm.orders.mapper.OrdersMapper;
import cn.tedu.crm.orders.pojo.entity.Orders;
import cn.tedu.crm.refund.mapper.RefundMapper;
import cn.tedu.crm.refund.pojo.dto.*;
import cn.tedu.crm.refund.pojo.entity.Refund;
import cn.tedu.crm.refund.pojo.vo.*;
import cn.tedu.crm.refund.service.RefundService;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RefundServiceImpl implements RefundService {
    @Autowired
    private RefundMapper refundMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CustomerFollowUpMapper customerFollowUpMapper;
    @Override
    public RefundVO selectById(Long id) {
        RefundVO refundVO = refundMapper.selectById(id);
        return refundVO;
    }

    @Override
    public ContractVO selectContract(Long id) {
        ContractVO contractVO = refundMapper.selectContract(id);
        return contractVO;
    }

    @Override
    public DepositVO selectDeposit(Long id) {
        DepositVO depositVO = refundMapper.selectDeposit(id);
        return depositVO;
    }

    @Override
    public CustomerVO selectCustomer(Long id) {
        CustomerVO customerVO = refundMapper.selectCustomer(id);
        return customerVO;
    }

    @Override
    @Loggable(module = OperationModule.RETURN_MANAGEMENT,type = OperationType.UPDATE)
    public void saveStatus(SaveStatusParam saveStatusParam) {
        //插入当前用户名和saveStatusParam
        UserNameVO userNameVO = refundMapper.selectUserName(StpUtil.getLoginIdAsLong());
        Refund refund = new Refund();
        BeanUtils.copyProperties(saveStatusParam,refund);
        refund.setOperator(userNameVO.getName());
        refundMapper.saveStatus(refund);

        DepositVO depositVO = refundMapper.selectDeposit(saveStatusParam.getId());
        String refundStatus = saveStatusParam.getRefundStatus();
        log.debug("修改退单状态，结果:{}","2".equals(refundStatus));
        if ("2".equals(refundStatus)){
            Orders orders = new Orders();
            orders.setDesignerProgress(20);
            orders.setUpdateTime(new Date());
            orders.setUpdateUser(StpUtil.getLoginIdAsLong());
            orders.setId(depositVO.getId());
            ordersMapper.update(orders);
            CustomerFollowUp customerFollowUp = new CustomerFollowUp();
            customerFollowUp.setStatus("60");
            customerFollowUp.setUpdateTime(new Date());
            log.debug("修改客户状态");
            customerFollowUp.setCustomerId(ordersMapper.selectCIDByOrderId(depositVO.getId()));
            log.debug("修改客户状态，结果：{}",customerFollowUpMapper.updateById(customerFollowUp));
            customerFollowUpMapper.updateByIdQ(customerFollowUp);
        }
    }

    @Override
    @Loggable(module = OperationModule.RETURN_MANAGEMENT,type = OperationType.EXAMINE)
    public void saveAudit1(SaveAudit1Param saveAudit1Param) {
        refundMapper.saveAudit1(saveAudit1Param);
    }

    @Override
    @Loggable(module = OperationModule.RETURN_MANAGEMENT,type = OperationType.EXAMINE)
    public void saveAudit2(SaveAudit2Param saveAudit2Param) {
        refundMapper.saveAudit2(saveAudit2Param);
    }

    @Override
    public List<RefundVO> selectRefundList(RefundQuery query) {
        List<RefundVO> select = refundMapper.select(query);
        log.debug("查询所有退款记录，：{}",select);
        return select;
    }


}
